﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace BlueLotus7.Common
{
    public class BaseConfigurationManager
    {
        protected static object xmlLock = new object();
        protected const string backupSuffix = ".bak.config";
        protected const string configurationRootNode = "iToni";
        protected const string configurationFileName = "configuration.xml";
        protected static XmlDocument xmlDoc = null;
        protected static XmlNode xmlSettings = null;

        protected string ConfigurationFilePath
        {
            get
            {
                return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configurationFileName);
            }
        }

        protected virtual string SectionRootName
        {
            get { return configurationRootNode; }
        }

        public BaseConfigurationManager()
        {
            try
            {
                LoadFile();
                LoadSection();
            }
            catch { }
        }

        protected void SaveFile()
        {
            lock (xmlLock)
            {
                try
                {
                    if (File.Exists(ConfigurationFilePath))
                    {
                        File.Copy(ConfigurationFilePath, ConfigurationFilePath + backupSuffix);
                    }

                    xmlDoc.Save(ConfigurationFilePath);
                }
                catch (Exception ex)
                {
                    throw new Exception("Fail to save and backup configuration", ex);
                }
            }
        }

        protected void LoadFile()
        {
            if (xmlDoc == null)
            {
                bool success = false;
                xmlDoc = new XmlDocument();
                try
                {
                    xmlDoc.Load(ConfigurationFilePath);
                    success = true;
                }
                catch { }

                if (!success)
                {
                    try
                    {
                        xmlDoc.Load(ConfigurationFilePath + backupSuffix);
                        success = true;
                    }
                    catch { }
                }

                if (success)
                {
                    try
                    {
                        xmlDoc.Save(ConfigurationFilePath);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Fail to save configuration with backup.", ex);
                    }
                }
                else
                {
                    throw new Exception("Fail to load configuration.");
                }
            }
        }

        protected void LoadSection()
        {
            xmlSettings = xmlDoc.SelectSingleNode(string.Format("descendant::{0}/{1}", configurationRootNode, SectionRootName));
        }

        public virtual void Save()
        {
            SaveFile();
        }
    }
}
